//
//  BloodOxygenDetailController.swift
//  DaRings
//
//  Created by 易先生 on 2022/11/2.
//

import UIKit
import MYModule

class BloodOxygenDetailController: BaseViewController {
    
    var provider: BloodOxygenProvider?
    
    /// 是否显示right item
    public var showRightItem: Bool = true
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigation.bar.backgroundColor = UIColor.background
        navigation.item.title = "blood_oxygen_title".language
        if showRightItem {
            navigation.item.rightBarButtonItem = rightBarButtonItem
        }
        
    }
    
    override func makeUI() {
        super.makeUI()
        
        view.addSubview(scrollView)
        scrollView.addSubview(contentView)
        contentView.addSubview(bloodOxygenView)
        contentView.addSubview(explainTitleView)
        contentView.addSubview(explainView)
        contentView.addSubview(sevenTimeChartView)
        
        scrollView.snp.makeConstraints { make in
            make.top.equalTo(topLayoutGuide.snp.bottom)
            make.leading.trailing.bottom.equalToSuperview()
        }

        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        
        
        bloodOxygenView.snp.makeConstraints { make in
            make.top.leading.trailing.equalToSuperview()
        }
        
        explainTitleView.snp.makeConstraints { make in
            make.top.equalTo(bloodOxygenView.snp.bottom).offset(20)
            make.leading.trailing.equalToSuperview()
        }
        
        explainView.snp.makeConstraints { make in
            make.top.equalTo(explainTitleView.snp.bottom)
            make.leading.trailing.equalToSuperview()
        }
        
        sevenTimeChartView.snp.makeConstraints { make in
            make.top.equalTo(explainView.snp.bottom).offset(20)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20)
        }
        
//        let valueFotmatter = DayAxisValueDateFormatter()
//        valueFotmatter.axisValueArray = ["90%","80%","99%","50%","99%","30%","99%"]
//        chartView.barChartView.setXAxis(valueFormatter: valueFotmatter)

        let config = self.setupConfig(isEmpty: true)
        sevenTimeChartView.barChartView.setConfig(config)
        sevenTimeChartView.barChartView.highlightFullBarEnabled = true
        sevenTimeChartView.barChartView.setBarCount(7)
//        chartView.barChartView.setData([90,80,99,50,99,30,99], barColor: Colors.blood_oxygen_main.withAlphaComponent(0.2), barHighLichtColor: Colors.blood_oxygen_main)
    }

    override func bindViewModel() {
        super.bindViewModel()
        
        rightBarButtonItem.rx.tap.subscribe { [weak self] _ in
            guard let self = self else {return}
            let vc = HistoryController()
            vc.showType = .bloodOxygen
            self.navigationController?.pushViewController(vc, animated: true)
        }.disposed(by: rx.disposeBag)
        
        bloodOxygenView.dateLbl.text = provider?.dateTitle
        
        if let last = provider?.last {
            
            bloodOxygenView.spo2Lbl.text = "\(last.o2)"
            bloodOxygenView.progressView.o2 = last.o2
            
        }
        
        if let sevenTimes = provider?.sevenTimes {
           
            
            let barHightLightColor = UIColor.BloodOxygen.theme
            let barNormalColor = UIColor.BloodOxygen.theme
            
            let barValues: [SevenTimeBarChartView.BarValue] = sevenTimes.map {
                
                SevenTimeBarChartView.BarValue(time: $0.end, data: Double($0.o2))
            }
            
            let config = self.setupConfig(isEmpty: false)
            let datas = barValues.map { $0.data }
            
            let xValueFormatter = DayAxisValueDateFormatter()
            xValueFormatter.axisValueArray = datas.map {
                
                if $0 == 0 {
                    return ""
                }
                
                return "\(Int($0))%"
                
            }
            
            
            let barModel = SevenTimeBarChartView.BarsModel(color: barNormalColor, hightLightColor: barHightLightColor, bars: barValues, xAxisValueFormatter: xValueFormatter, xConfig: config, YMaxValue: 100)
            
            sevenTimeChartView.barModel = barModel
            
        }
        
        
    }
    
    private func setupConfig(isEmpty: Bool) -> DFBarChartViewConfig {
        let config = DFBarChartViewConfig.init(
            xAxisLabelFont: Fonts.systemFont(ofSize: 10),
            xAxisLabelTextColor: isEmpty == false ? UIColor.assist_222222 : UIColor.assist_FFFFFF,
            xAxisLineColor: UIColor.BloodOxygen.theme,
            xAxisLineHeight: 1,
            highlightPerTapEnabled: true)
        return config
    }
    
    //MARK: - lazy load -

    public lazy var rightBarButtonItem: UIBarButtonItem = {
        let rightBarButtonItem = UIBarButtonItem()
        rightBarButtonItem.image = UIImage(named: "ic_data")
        return rightBarButtonItem
    }()
    
    public lazy var scrollView: ScrollView = {
        let view = ScrollView()
        view.showsVerticalScrollIndicator = false
        view.backgroundColor =  UIColor.darkBackground
        view.bounces = false
        return view
    }()

    public lazy var contentView: View = {
        let view = View()
        view.backgroundColor =  UIColor.darkBackground
        return view
    }()
    
    private lazy var bloodOxygenView: BloodOxygenDetailView = {
        let bloodOxygenView = BloodOxygenDetailView()
        bloodOxygenView.backgroundColor = UIColor.background
        bloodOxygenView.setRoundedCorners(.bottom, radius: 18)
        return bloodOxygenView
    }()
    
    
    private lazy var explainTitleView: BloodOxygenDetailTitleView = {
        let explainTitleView = BloodOxygenDetailTitleView()
        explainTitleView.bind(to: "blood_oxygen_about".language, bgColor: UIColor.background)
        explainTitleView.setRoundedCorners(.top, radius: 18)
        return explainTitleView
    }()
    
    private lazy var explainView: ExPlainView = {
        let explainView = ExPlainView()
        explainView.backgroundColor = UIColor.background
        explainView.updateData(
            textColor: UIColor.BloodOxygen.theme,
            title: "blood_oxygen_know".language,
            explain: "blood_oxygen_about_description".language,
            explainTextColor: UIColor.BloodOxygen.theme.withAlphaComponent(0.8),
            bgColor: UIColor.BloodOxygen.theme
        )
        explainView.setRoundedCorners(.bottom, radius: 18)
        return explainView
    }()
    
    private lazy var sevenTimeChartView: SevenTimeBarChartView = {
        let chartView = SevenTimeBarChartView()
        chartView.backgroundColor = UIColor.background
        chartView.setRoundedCorners(.all, radius: 18)
        return chartView
    }()
}
